from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_plant.entity.do.planter_do import PlantPlanter
from module_plant.entity.vo.planter_vo import PlanterModel, PlanterPageQueryModel
from utils.page_util import PageUtil


class PlanterDao:
    """
    种植主体信息模块数据库操作层
    """

    @classmethod
    async def get_planter_detail_by_id(cls, db: AsyncSession, planter_id: int):
        """
        根据种植主体ID获取种植主体信息详细信息

        :param db: orm对象
        :param planter_id: 种植主体ID
        :return: 种植主体信息信息对象
        """
        planter_info = (
            (
                await db.execute(
                    select(PlantPlanter)
                    .where(
                        PlantPlanter.planter_id == planter_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return planter_info

    @classmethod
    async def get_planter_detail_by_info(cls, db: AsyncSession, planter: PlanterModel):
        """
        根据种植主体信息参数获取种植主体信息信息

        :param db: orm对象
        :param planter: 种植主体信息参数对象
        :return: 种植主体信息信息对象
        """
        planter_info = (
            (
                await db.execute(
                    select(PlantPlanter).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return planter_info

    @classmethod
    async def get_planter_list(cls, db: AsyncSession, query_object: PlanterPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取种植主体信息列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 种植主体信息列表信息对象
        """
        query = (
            select(PlantPlanter)
            .where(
                PlantPlanter.year == query_object.year if query_object.year else True,
                PlantPlanter.planter_name.like(f'%{query_object.planter_name}%') if query_object.planter_name else True,
                PlantPlanter.planter_type == query_object.planter_type if query_object.planter_type else True,
                PlantPlanter.county.like(f'%{query_object.county}%') if query_object.county else True,
            )
            .order_by(PlantPlanter.planter_id)
            .distinct()
        )
        planter_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return planter_list

    @classmethod
    async def add_planter_dao(cls, db: AsyncSession, planter: PlanterModel):
        """
        新增种植主体信息数据库操作

        :param db: orm对象
        :param planter: 种植主体信息对象
        :return:
        """
        db_planter = PlantPlanter(**planter.model_dump(exclude={}))
        db.add(db_planter)
        await db.flush()

        return db_planter

    @classmethod
    async def edit_planter_dao(cls, db: AsyncSession, planter: dict):
        """
        编辑种植主体信息数据库操作

        :param db: orm对象
        :param planter: 需要更新的种植主体信息字典
        :return:
        """
        await db.execute(update(PlantPlanter), [planter])

    @classmethod
    async def delete_planter_dao(cls, db: AsyncSession, planter: PlanterModel):
        """
        删除种植主体信息数据库操作

        :param db: orm对象
        :param planter: 种植主体信息对象
        :return:
        """
        await db.execute(delete(PlantPlanter).where(PlantPlanter.planter_id.in_([planter.planter_id])))

